package com.chinasoft.movie.controller;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/")
public class RedisController {


    @Autowired
    private ValueOperations redisValue;

//    @RequestMapping("/getRedisData")
//    public String getRedisData() {
////        ValueOperations<String, Object> redisString = template.opsForValue();
////        // SET key value: 设置指定 key 的值
////        redisString.set("strKey1", "hello spring boot redis");
////        System.out.println(redisString.get("strKey1"));
////        Map<String, Object> map = new HashMap<>();
////        map.put("id", "10010");
////        map.put("name", "redis_name");
////        map.put("amount", 12.34D);
////        map.put("age", 11);
////        redisHash.putAll("hashKey", map);
////        // HGET key field 获取存储在哈希表中指定字段的值
////        String name = (String) redisHash.get("hashKey", "name");
////        System.out.println(name);
//
//       //ValueOperations<String, Object> redisString = redisTemplate.opsForValue();
//        redisValue.set("strKey1", "hello spring boot redis1");
//        redisValue.set("strKey2", "hello spring boot redis2");
//        redisValue.set("strKey3", "hello spring boot redis3");
//        //System.out.println(redisString.get("strKey1"));
//        //redisTemplate.hasKey("strKey1");
//        //System.out.println(redisString.size("strKey1"));//key+v的值(整个长度的值)//针对与key和value进行put和get
//        //如果需要判断这个redis是否含有key等方法需要用到的就是redisTemplate
//        //redisString是一个针对数据进行操纵的对象
//        //System.out.println(redisString.);
//        //System.out.println(redisTemplate.hasKey("strKey1"));//true/false
//        //System.out.println(redisTemplate.delete("strKey3"));//如果有这个key返回true如果没这个key返回false
//
//
//
////        Set<String> keys = redisTemplate.keys("strKey*");//匹配某种key的命名方式
////        Iterator<String> it1 = keys.iterator();
////        while (it1.hasNext()) {
////            System.out.println("原来的Key"+it1.next());
////        }
////        //keySet Entry<K,V> entrySet
////        String [] s={"strKey1","strKey2"};
////        for (int i=0;i<s.length;i++){
////            redisTemplate.delete(s[i]);
////        }
////
////        Set<String> keys1 = redisTemplate.keys("strKey*");//匹配某种key的命名方式
////        Iterator<String> it2 = keys1.iterator();
////        while (it2.hasNext()) {
////            System.out.println("后来的key"+it2.next());
////        }
//        //expireKey
//        //redisTemplate.expire("strKey1",1, TimeUnit.MINUTES);
////        Set<String> keys = redisTemplate.keys("strKey*");//匹配某种key的命名方式
////        Iterator<String> it1 = keys.iterator();
////        while (it1.hasNext()) {
////            System.out.println("原来的Key"+it1.next());
////        }
//
//       // Set<String> keys = redisTemplate.keys("*");
//       // redisTemplate.delete(keys);
//
//
//
//        return "redis";
//    }

    @RequestMapping("/getRedisData")
    public String getRedisData() {
        List<Map<String,Object>> list = null;
        if(redisValue.get("newsList")==null){
            //redis里面没有任何newsList的数据
            //问我需要连接数据库将tb_news表中的数据取出来然后放入到newsList这个key中
            System.out.println("redis里面没有任何的新闻列表新信息,我需要从数据库查询数据");
            list=new ArrayList<Map<String,Object>>();//调用service层进行查询数据
            for(int i=0;i<5;i++){
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("newsId",i+1);
                map.put("newsTitle","茅台股价又涨了"+i);
                map.put("newsTime","2021-7-2"+i);
                list.add(map);
            }
            redisValue.set("newsList",list);
        }else{
            //redis里面newsList是有值的
            list=(List<Map<String,Object>>)redisValue.get("newsList");
        }
        //将这个集合的数据转发到对应的页面
        //map.put("list",list);
//return new ModelAndView("news",map);
        return "redis";

}

}
